//
//  UINavigationBar+Awesome.m
//  TLYL
//
//  Created by MC on 2017/9/19.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "UINavigationBar+Awesome.h"
#import <objc/runtime.h>
#define kStatusTopNavDifference (kIs_iPhoneX ? 44.f : 20.f)

@implementation UINavigationBar (Awesome)
static char overlayKey;

- (UIView *)overlay
{
    return objc_getAssociatedObject(self, &overlayKey);
}

- (void)setOverlay:(UIView *)overlay
{
    objc_setAssociatedObject(self, &overlayKey, overlay, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)vip_lt_setBackgroundColor:(UIColor *)backgroundColor
{
    if (!self.overlay) {
        [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        
        self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, -kStatusTopNavDifference, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) + kStatusTopNavDifference)];
        
        
        self.overlay.userInteractionEnabled = NO;
        self.overlay.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [self insertSubview:self.overlay atIndex:0];
        
    }
    self.overlay.backgroundColor = backgroundColor;
}
- (void)lt_setBackgroundColor:(UIColor *)backgroundColor
{
    if (!self.overlay) {
        
        
        [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, -kStatusTopNavDifference, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) + kStatusTopNavDifference)];
        
        
        self.overlay.userInteractionEnabled = NO;
        self.overlay.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [self insertSubview:self.overlay atIndex:0];
        
        if (G_SCREENHEIGHT == 812) {

        }else{
            UIImageView * imgVL = [[UIImageView alloc]init];
            //        imgVL.backgroundColor=[UIColor orangeColor];
            imgVL.image=[UIImage imageNamed:@"home-icon-more"];
            [self.overlay addSubview:imgVL];
            if (G_SCREENWIDTH>400) {
                imgVL.frame=CGRectMake(9+4, 20+11, 22, 22);
            }else{
                imgVL.frame=CGRectMake(9, 20+11, 22, 22);
            }
        }
            
        
        
    }
    self.overlay.backgroundColor = backgroundColor;
}

- (void)lt_setTranslationY:(CGFloat)translationY
{
    self.transform = CGAffineTransformMakeTranslation(0, translationY);
}

- (void)lt_setElementsAlpha:(CGFloat)alpha
{
    [[self valueForKey:@"_leftViews"] enumerateObjectsUsingBlock:^(UIView *view, NSUInteger i, BOOL *stop) {
        view.alpha = alpha;
    }];
    
    [[self valueForKey:@"_rightViews"] enumerateObjectsUsingBlock:^(UIView *view, NSUInteger i, BOOL *stop) {
        view.alpha = alpha;
    }];
    
    UIView *titleView = [self valueForKey:@"_titleView"];
    titleView.alpha = alpha;
    //    when viewController first load, the titleView maybe nil
    [[self subviews] enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL *stop) {
        if ([obj isKindOfClass:NSClassFromString(@"UINavigationItemView")]) {
            obj.alpha = alpha;
            *stop = YES;
        }
    }];
}

- (void)lt_reset
{
    [self setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.overlay removeFromSuperview];
    self.overlay = nil;
}

@end


